package com.laysonx.easy.invoke.core;

import com.laysonx.easy.invoke.controller.EasyInvokeController;
import com.laysonx.easy.invoke.support.InvokeMonitor;
import com.laysonx.easy.invoke.support.ResourcePool;
import com.laysonx.easy.invoke.util.BeanRegistrationUtil;
import com.laysonx.easy.invoke.util.SpringContextUtil;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.HashMap;
import java.util.Map;

@Data
public class EasyInvokeConfigurer implements BeanDefinitionRegistryPostProcessor,ResourceLoaderAware {

    private static String[] scanController= {"com.laysonx.easy.invoke.controller"};

    private ResourceLoader resourceLoader;

    private String password;

    private Boolean isSpringBoot = true;

    private String domain = "" ;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

        ControllerScanner scanner = new ControllerScanner(registry);
        scanner.setResourceLoader(resourceLoader);
        scanner.registerFilters();
        scanner.doScan(scanController);

        Map<String, Object> property = new HashMap<>(2);
//        property.put("password", password);
        property.put("domain",domain);

//        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ResourcePool.class.getName(), ResourcePool.class, property);
//        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ResourcePoolProcessor.class.getName(), ResourcePoolProcessor.class);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringContextUtil.class.getName(), SpringContextUtil.class);

        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, InvokeMonitorProcessor.class.getName(), InvokeMonitorProcessor.class);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, InvokeMonitor.class.getName(), InvokeMonitor.class,property);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DefaultInvokePostProcessor.class.getName(), DefaultInvokePostProcessor.class);

        if (!isSpringBoot) {
            BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ThymeleafViewResolver.class.getName(), ThymeleafViewResolver.class);
            BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ThymeleafViewResolverPostProcessor.class.getName(), ThymeleafViewResolverPostProcessor.class);
        }

        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, EasyInvokeController.class.getName(), EasyInvokeController.class);

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

}